<?php
require 'HTTPApi.php';
require 'FixxExceptions.php';

class fixxAPI extends httpapi{
	private $headers=array(
		"Accept"=>"application/",
		"Content-Type"=>"application/"
	);
	
	public function __construct($options){
		$this->options=$options;
		$this->headers["Accept"]=$this->headers["Accept"]."".$options['type'];
		$this->headers["Content-Type"]=$this->headers["Content-Type"]."".$options['type'];
	}
	
	private function handle_response($response){
		if($response->status == 200){
			return json_decode($response->responseText,true);
		}else{
			throw new Exception($ajax->statusText);
		}
	}

	/**
	*	Get all projects
	*	GET /api/projects/
	*/
	public function getProjects(){
		$request=array(
			"url"=>"/api/projects",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get a specific project
	*	GET /api/projects/{project_id}
	*/
	public function getProject($project_id){
		$request=array(
			"url"=>"/api/projects/".$project_id,
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Create a new project
	*	POST /api/projects/
	*	Data - project
	*/
	public function newProject($project){
		$request=array(
			"url"=>"/api/projects/",
			"headers"=>$this->headers,
			"data"=>json_encode($project)
			);
			
		return $this->handle_response($this->post($request));
	}

	/**
	*	Update a project
	*	PUT /api/projects/{project_id}
	*	Data - project
	*/	
	public function saveProject($project){
		$id=$project['id'];
		$project['id']=NULL;
		
		$request=array(
			"url"=>"/api/projects/".$id,
			"headers"=>$this->headers,
			"data"=>json_encode($project)
			);
			
		return $this->handle_response($this->put($request));
	}

	/**
	*	Get all resolutions for a project
	*	GET /api/projects/{project_id}/resolutions
	*/
	public function getResolutions($project_id){
		$request=array(
			"url"=>"/api/projects/".$project_id."/resolutions",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get all issue types for a project
	*	GET /api/projects/{project_id}/issue-types
	*/
	public function getIssueTypes($project_id){
		$request=array(
			"url"=>"/api/projects/".$project_id."/issue-types",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get all areas for a project
	*	GET /api/projects/{project_id}/areas
	*/
	public function getAreas($project_id){
		$request=array(
			"url"=>"/api/projects/".$project_id."/areas",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get all versions for a project
	*	GET /api/projects/{project_id}/versions
	*	TODO - Get only released versions for a project
	*	GET /api/projects/{project_id}/versions?type=fixfor
	*/
	public function getVersions($project_id,$released=false){
		if($released==true){
			$params=array(
				"type"=>"fixfor"
			);
		}
		
		$request=array(
			"url"=>"/api/projects/".$project_id."/versions",
			"params"=>$params,
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get all issues in the system (with filtering, search & paging)
	*	GET /api/issues?{querystring}
	*
	*	FILTERING
	*	qProject				Project										Project ID
	*	qArea					Area										Area ID
	*	qResolution				Resolution									Resolution ID
	*	qType					Issue Type									Issue Type ID
	*	qAffected				Affected Version							Version ID
	*	qFixFor	Fix				For Version									Version ID
	*	qAssignedTo				Assigned To									User ID
	*	qCreatedBy				Created By									User ID
	*	qWatcher				People watching an issue					User ID
	*	qResolvedBy				Resolved By									UserID
	*	qPriority				Priority									Priority ID
	*	qStatus	Status			One of										"Open", "Resolved", "Closed", "All"
	*	qCreatedOnLowerRange	Created On greater than provided value		Date string in the format of "dd/mm/yyyy"
	*	qCreatedOnUpperRange	Created On less than provided value			Date string in the format of "dd/mm/yyyy"
	*	qCreatedOnLowerRange	Created On less than provided value			Date string in the format of "dd/mm/yyyy"
	*	qDueOnLowerRange		Due On greater than provided value			Date string in the format of "dd/mm/yyyy"
	*	qDueOnUpperRange		Due On less than provided value				Date string in the format of "dd/mm/yyyy"
	*	qResolvedOnLowerRange	Resolved On greater than provided value		Date string in the format of "dd/mm/yyyy"
	*	qResolvedOnUpperRange	Resolved On less than provided value		Date string in the format of "dd/mm/yyyy"
	*
	*	SORTING
	*	qSortBy			id | priority | type | project | title | assignedTo
	*	qSortOrder		1 (Ascending order) | 2 (Descending order)
	*
	*	Paging
	*	qCurrentPage	number
	*	qPageSize		number (The default page size is 50)
	*/
	public function getIssues($filter=array()){
		$request=array(
			"url"=>"/api/issues",
			"params"=>$filter,
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get specific issue
	*	GET /api/issues/{issue_id}
	*/
	public function getIssue($issue_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id,
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	public function saveIssue($issue){
		$id=$issue['id'];
		$issue['id']=NULL;
		
		$request=array(
			"url"=>"/api/issues/".$id,
			"headers"=>$this->headers,
			"data"=>json_encode($issue)
			);
		
		return $this->handle_response($this->put($request));	
	}
	
	public function newIssue($issue){
		$request=array(
			"url"=>"/api/issues/",
			"headers"=>$this->headers,
			"data"=>json_encode($issue)
			);
		return $this->handle_response($this->post($request));
	}
	
	public function deleteIssue($issue_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id,
			"headers"=>$this->headers
			);
		return $this->handle_response($this->delete($request));
	}
	
	/**
	*	Get all comments for an issue - GET /api/issues/{issue_id}/comments
	*	GET /api/issues/{issue_id}/comments
	*/	
	public function getComments($issue_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/comments",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	public function postComment($issue_id,$comment){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/comments",
			"headers"=>$this->headers,
			"data"=>json_encode($comment)
			);

		return $this->handle_response($this->post($request));
	}
	
	public function deleteComment($issue_id,$comment_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/comments/".$comment_id,
			"headers"=>$this->headers
			);

		return $this->handle_response($this->delete($request));
	}
	
	/**
	*	Get all timelogs for an issue - Returns all timelogs for an issue identified by issue_id
	*	GET /api/issues/{issue_id}/timelogs
	*/
	public function getTimelogs($issue_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/timelogs",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	public function newTimelog($issue_id,$timelog){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/timelogs",
			"headers"=>$this->headers,
			"data"=>json_encode($timelog)
			);
			
		return $this->handle_response($this->post($request));
	}
	
	/**
	*	Get all tags for an issue - Returns all tags for an issue identified by issue_id
	*	GET /api/issues/{issue_id}/tags
	*/
	public function getTags($issue_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/tags",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	public function newTag($issue_id,$tag){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/tags",
			"headers"=>$this->headers,
			"data"=>json_encode($tag)
			);
			
		return $this->handle_response($this->post($request));
	}
	
	public function deleteTag($issue_id,$tag_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/tags/".$tag_id,
			"headers"=>$this->headers
			);

		return $this->handle_response($this->delete($request));
	}
	
	/**
	*	Get all changelogs for an issue
	*	GET /api/issues/{issue_id}/changelogs
	*/
	public function getChangelogs($issue_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/changelogs",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get all users watching an issue
	*	GET /api/issues/{issue_id}/watchers
	*/
	public function getWatchers($issue_id){
		$request=array(
			"url"=>"/api/issues/".$issue_id."/watchers",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get all users in the system
	*	GET /users
	*/
	public function getUsers(){
		$request=array(
			"url"=>"/api/users/",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get a specific user in the system - Allows you to pass either a user id or username to retrieve details.
	*	GET /users/{user_id} or GET /users/{username}
	*/
	public function getUser($user_id){
		$request=array(
			"url"=>"/api/users/".$user_id,
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}

	/**
	*	Get all users assigned specifically to a project (only applicable to restricted projects)
	*	GET /api/projects/{project_id}/users
	*/
	public function getUsersForProject($project_id){
		$request=array(
			"url"=>"/api/projects/".$project_id."/users/",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));		
	}
	
	/**
	*	Get all clients assigned to a project
	*	GET /api/projects/{project_id}/clients
	*/
	public function getClientsForProject($project_id){
		$request=array(
			"url"=>"/api/projects/".$project_id."/clients/",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get all priorities in the system
	*	GET /priorities
	*/
	public function getPriorities(){
		$request=array(
			"url"=>"/api/priorities/",
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
	
	/**
	*	Get a specific priority in the system
	*	GET /priorities/{priority_id}
	*/
	public function getPriority($priority_id){
		$request=array(
			"url"=>"/api/priorities/".$priority_id,
			"headers"=>$this->headers
			);

		return $this->handle_response($this->get($request));
	}
}
?>